package switchviz.graphic.signal;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.jdom.Element;

import switchviz.core.signal.Signal;

public class ComplexSignal extends Signal implements IGaugeSignal
{
	public static final int OPERATION_ADD = 0;
	public static final int OPERATION_SUB = 1;
	public static final int OPERATION_MUL = 2;
	public static final int OPERATION_DIV = 3;
	
	private ComplexSignalDataModel _datamodel = null;
	private ComplexSignalPersistence _persistenceHandler = null;
	private ComplexSignalPanel _testpanel = null;
	private boolean _enable = true;
	
	private IGaugeSignal _firstsignal = null;
	private IGaugeSignal _secondsignal = null;
	private List<String> _availableSignals = new ArrayList<String>();
	
	public ComplexSignal(Element node)
	{
		_persistenceHandler = new ComplexSignalPersistence(node);
		_datamodel = _persistenceHandler.getDataModelFromElement();
		_datamodel.setType(BasicSignalDataModel.TYPECOMPLEX);
		this.setAliasName("busyPer");
	}
	
	public ComplexSignal(Element node, String name)
	{
		this(node);
		_datamodel.setName(name);
		_persistenceHandler.setDataModel(_datamodel);
	}
	
	private ComplexSignalPanel getSimplePanel()
	{
		if(_testpanel == null)
			_testpanel = new ComplexSignalPanel(_persistenceHandler.getElement(),  _availableSignals);

		return _testpanel;
	}
	
	public float[] getColor() 
	{
		return _datamodel.getColor();
	}

	public String getName() 
	{
		return _datamodel.getName();
	}

	public float getValue() 
	{
		float value = 0.0f;
		
		if (!_firstsignal.isEnable() && !_secondsignal.isEnable())
		{
			_enable = false;
			return 0.0f;
		}
		
		value = _firstsignal.getValue();
		
			switch (_datamodel.getOperation())
			{
			case OPERATION_ADD:
				value += _secondsignal.getValue();
				break;
			case OPERATION_SUB:
				div(value, _secondsignal.getValue());
				break;
			case OPERATION_MUL:
				value *= _secondsignal.getValue();
				break;
			case OPERATION_DIV:
				value /= _secondsignal.getValue();
				break;
			}
		
		return value*_datamodel.getCoefficient() + _datamodel.getOffset();
	}
	
	private float div(float a, float b)
	{
		if (b == 0)
			return Float.MAX_VALUE;
		else 
			return (a/b);
	}

	public Element getElement() 
	{
		return getSimplePanel().getElement();
	}

	public JPanel getPanel() 
	{
		return getSimplePanel();
	}

	public int getAlarmmode() 
	{
		return _datamodel.getAlarmmode();
	}

	public float getMax() 
	{
		return _datamodel.getMax();
	}

	public float getMin() 
	{
		return _datamodel.getMin();
	}

	public boolean isLogarithmic() 
	{
		return _datamodel.isLogarithm();
	}

	public int getPriority() 
	{
		return _datamodel.getPriority();
	}

	public String getType() 
	{
		return _datamodel.getType();
	}
	
	public void addSimpleSignal(IGaugeSignal signal)
	{
		String name = signal.getName();
		
		if (name.equals(_datamodel.getFirstalias()))
			_firstsignal = signal;
		if (name.equals(_datamodel.getSecondalias()))
			_secondsignal = signal;
		
		addSimpleSignal(name);
	}
	
	public void addSimpleSignal(String signal)
	{
		_availableSignals.add(signal);
	}

	public boolean isEnable() 
	{
		return _enable;
	}
}
